
module WindowModifyCustomer where

import Graphics.UI.Gtk
import Customer
import Data.IORef
import MainHandler
import Data.Acid
import Repositorium


    
--windowModifyCustomerAcid :: (IO Customer) -> Combo -> AcidState (EventState (AcidState Repositorium -> IO uf10)) -> IO ()
windowModifyCustomerAcid iocustomer combo state = do

    customer <- iocustomer
    initGUI
    window <- windowNew
    windowSetPosition window WinPosCenter
    
    labelCustomerTitle <- labelNew (Just "Cliente:")
    labelCustomer <- labelNew (Just (getCustomerName customer))
    labelNewName <- labelNew (Just "Nuevo nombre:")
    
    entryName <- entryNew
    
    buttonAceptar <- buttonNewWithLabel "Aceptar"
    onClicked buttonAceptar (modifyCustomerAcid customer state entryName window combo)
    buttonVolver <- buttonNewWithLabel "Volver"
    onClicked buttonVolver (widgetDestroy window)
    
    vbox <- vBoxNew False 10
    
    set window [windowTitle := "Modificar cliente",windowDefaultWidth := 150,
        windowDefaultHeight := 150, containerBorderWidth := 10, containerChild := vbox]
  

      
    boxPackStart vbox labelCustomerTitle PackNatural 1
    boxPackStart vbox labelCustomer PackNatural 1
    boxPackStart vbox labelNewName PackNatural 1
    boxPackStart vbox entryName PackNatural 1
    boxPackStart vbox buttonAceptar PackNatural 1
    boxPackStart vbox buttonVolver PackNatural 1
    
    
    widgetShowAll window
    
    
----------------------------------------------------------------------------
-------------------- FUNCIONES DE CONTROL CON ACIDSTATE------------------------

modifyCustomerAcid customer state entry win combo = do
    newName <- entryGetText entry
    updateNameToCustomer customer newName state
    addMsg <- (messageDialogNew (Just win) [DialogDestroyWithParent] MessageInfo ButtonsOk "Cliente modificado")
    dialogRun addMsg
    widgetDestroy win 
    updateComboAcid combo state
    return ()

updateComboAcid combo state = do
    customers <- getCustomersAcid state
    store <- listStoreNew (customers)
    comboBoxSetModel combo (Just store)
    customStoreSetColumn store textColumn3 (getCustomerName)
    
---------------------------------------------------------------------------
-----------------FUNCIONES DE CONTROL--------------------------------------
---------------------------------------------------------------------------
     
    

        
changeCtm customer newName (c:cs) = 
    if ((getCustomerName customer) == (getCustomerName c))
        then (setCustomerName c newName): cs
        else c : (changeCtm customer newName cs)
    
 
    
textColumn3 :: ColumnId Customer String
textColumn3 = makeColumnIdString 0

